package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.annotation.AddCacheAnnotation;
import com.baizhi.cmfz.annotation.DeleteCacheAnnotation;
import com.baizhi.cmfz.dao.ArticleDao;
import com.baizhi.cmfz.entity.Article;
import com.baizhi.cmfz.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * This is a Class.
 *
 * @author zhanglh
 * @date 2019/09/11
 */
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDao articleDao;

    @Override
    @Transactional(readOnly = true)
    @AddCacheAnnotation
    public Map selectAllArticle(int page, int limit, String articleName) {
        page = (page - 1) * limit;
        Map<String, Object> map = new HashMap<>();
        map.put("data", articleDao.selectAllByPage(page, limit, articleName));
        map.put("count", articleDao.countArticle(articleName));
        map.put("code", 0);
        return map;
    }

    @Override
    @DeleteCacheAnnotation
    public int addArticle(Article article) {
        return articleDao.insertArticle(article);
    }

    @Override
    @DeleteCacheAnnotation
    public int removeArticle(int[] ids) {
        return articleDao.deleteArticle(ids);
    }

    @Override
    @DeleteCacheAnnotation
    public int updateArticle(Article article) {
        return articleDao.updateArticle(article);
    }

    @Override
    @DeleteCacheAnnotation
    public int freeze(Article article) {
        if (article.getArticleStatus() == 0) {
            article.setArticleStatus(1);
        }
        if (article.getArticleStatus() == 1) {
            article.setArticleStatus(0);
        }
        return articleDao.updateArticle(article);
    }

    @Override
    @Transactional(readOnly = true)
    @AddCacheAnnotation
    public Article selectOneArticle(int articleId) {
        return articleDao.selectArticleById(articleId);
    }

    @Override
    @DeleteCacheAnnotation
    public int freezeStutas(int guruId, int status) {
        return articleDao.updateArticleStatusByGuruId(guruId, status);
    }

    @Override
    public List<Article> fundArticleList(Integer count) {
        if (count == 0) {
            count = null;
        }
        return articleDao.fundArticleList(count);
    }
}
